<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<input name="text" type="text"  id="inputText"  >
<input type="button" name="发送"  value="发送" id="sendText">
</body>
<script>
    ws = new WebSocket("ws://127.0.0.1:2000");
    ws.onopen = function() {
        ws.send('tom');
        alert("给服务端发送一个字符串：tom");
    };
    ws.onmessage = function(e) {
        alert("收到服务端的消息：" + e.data);
    };
    var sendText = document.getElementById('sendText');
    var inputText = document.getElementById('inputText');
    sendText.onclick = function () {
        ws.send(inputText.value);
    }
</script>
</html>